Event.observe(window, 'load', function() {
});

function addCompany(){
  var company_name=$('company_title').getValue()
  try {
    if (company_name != ""){
    Element.insert('companies', { bottom: "<div class='company' style='position:relative;'>\n" + company_name + "\n<span class='task'><a href='#' onclick=\"$(this).up('.company').remove(); return false;\" style='position:absolute;top:0px;right:3px;'>remove</a></span><input type='hidden' name='complaint[all_companies][]' value='"+ current_selected_company_id +"'/></div>" });
    }  
  } 
  catch (e) { alert('RJS error:nn')}; 
  return false;
}  
function addTag(){
  var tag_name=$('tag_title').getValue()
  try {
    if (tag_name != ""){
    Element.insert('tags', { bottom: "<div class='tag' style='position:relative;'>\n" + tag_name + "\n<span class='task'><a href='#' onclick=\"$(this).up('.tag').remove(); return false;\" style='position:absolute;top:0px;right:3px;'>remove</a></span><input type='hidden' name='complaint[all_tags][]' value='"+ current_selected_tag_id +"'/></div>" });
    }  
  } 
  catch (e) { alert('RJS error:nn')}; 
  return false;
} 
//function addNewCompany(){
//  Dialog.confirm($('new_company').innerHTML, {className:"alphacube", width:400, okLabel: "Create", cancelLabel: "cancel", 
//    onOk:function(win){ 
//      $('error_msg').innerHTML='Information given';
//      $('error_msg').show(); Windows.focusedWindow.updateHeight(); 
//      new Effect.Shake(Windows.focusedWindow.getId()); 
//      return false;}}); 
//} 
var current_selected_company_id="";
var current_selected_tag_id="";

function getSelectionId(text,li){
  current_selected_company_id=li.id;
}
function getTagSelectionId(text,li){
  current_selected_tag_id=li.id;
}

function support_complaint(id){
  var complaint_div=$("comp_"+id)
  url='/complaints/support?complaint_id='+id
  new Ajax.Request(url, {
    onSuccess: function(response) {
      count=response.responseJSON.count;
      complaint_div.down('.support_task').innerHTML="Supported"
      complaint_div.down('.support_count').innerHTML=count
       }
     });
}
//################## visualization ################################
//global variables
var vis_type="";
var data;
function load_visualization(visualization_type){
    vis_type=visualization_type
    var query = new google.visualization.Query('home/visualization');
    query.setQuery(visualization_type);
		query.send(handleQueryResponse);
		}

function handleQueryResponse(response) {	
  if (response.isError()) {alert("Error with the query response");}
		data = response.getDataTable();
		$('visualization_div').innerHTML="";
		table = new google.visualization.ColumnChart($('visualization_div'));
    table.draw(data, 
					{width:350, height:250,legend: 'bottom',isVertical:true,isStacked:false,axisColor:'#b8b7b7',
					colors:['#d56021']});
		google.visualization.events.addListener(table, 'select', vis_event_handler)
  }    
      
  function vis_event_handler() {
    var selection = table.getSelection();
    company_name=data.getValue(selection[0].row,0)
    window.location.href="complaints/search?filters[]"+vis_type+"="+company_name
	  //var x=document.getelementbyid("winner");
		//x.innerhtml="winner is"+data.getvalue(row, 0)
	}

